<template>
	<view class="container">
		<!-- 弹出消息 -->
		<uv-toast ref="uToast"></uv-toast>
		<!-- 头部导航栏 -->
		<view class="header">
			<view class="nav-back" @click="goBack">
				<uv-icon name="arrow-left" size='32'></uv-icon>
			</view>
			<view class="header-icon">
				<uv-icon name="download" size='28'></uv-icon>
				<uv-icon name="share-square" size='28'></uv-icon>
				<uv-icon name="list" size='28'></uv-icon>
			</view>
		</view>
		<!-- 书籍主信息 -->
		<view class="book-main">
			<image :src="book.cover" class="cover" mode="aspectFill" />
			<view class="info">
				<view class="title">{{ book.title }}</view>
				<view class="author">{{ book.author }}</view>
				<view class="meta">
					<text>{{ book.category }}</text>
					<text>{{ book.detail }}</text>
					<text>{{ book.wordCount }}万字</text>
				</view>
			</view>
		</view>

		<!-- 书籍标签 -->
		<view class="tag-group">
			<view v-for="(tag, index) in book.tags" :key="index" class="tag">
				{{ tag }}
			</view>
		</view>

		<!-- 评分信息 -->
		<view class="rating-info">
			<view class="score">
				<text class="number"><text>{{ book.score }}</text>分</text>
				<text>{{ book.reviewCount }}万人点评</text>
			</view>
			<view class="counts">
				<text><text>{{ book.readingCount }}</text>万人</text>
				<text>正在阅读</text>
			</view>
		</view>

		<!-- 书评区块 -->
		<view class="reviews">
			<view class="header-reviews">
				<view class="header-title">
					<text class="title">书评</text>
					<text class="count">({{ book.commentCount }}条)</text>
				</view>
				<uv-button size="mini" custom-style="border-radius: 10px;
				color: #fff;
				background-color: #f59a23;" @click="gotomorecommentsView">
					写书评
				</uv-button>
			</view>

			<!-- 书评列表 -->
			<view class="comment-list">
				<view v-for="(comment, index) in book.comments" :key="index" class="comment-item">
					<image :src="comment.avatar" class="avatar" />
					<view class="content">
						<view class="user">
							<view class="user-rate">
								<text class="name">{{ comment.username }}</text>
								<uv-rate :value="comment.rating" disabled size="14" />
							</view>
							<view class="user-more">
								<uv-icon name="more-dot-fill"></uv-icon>
							</view>
						</view>

						<view class="text">{{ comment.content }}</view>
						<view class="footer">
							<text class="time">{{ comment.time }}</text>
							<view class="actions">
								<view class="action">
									<uv-icon name="chat" size="14" />
									<text>{{ comment.replyCount }}</text>
								</view>
								<view class="action">
									<uv-icon name="thumb-up" size="14" />
									<text>{{ comment.likeCount }}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 查看全部评论 -->
		<view class="view-all" @click="gotomorecommentsView">
			<text>查看全部评论</text>
			<uv-icon name="arrow-right" size="28"></uv-icon>
		</view>

		<!-- 底部操作栏 -->
		<view class="toolbar">
			<view class="tool" @click="open">
				<uv-icon name="list" size="24" />
				<text>目录</text>
			</view>
			<view class="tool" @click="handleBookshelf">
				<uv-icon :name="isInBookshelf ? 'star-fill' : 'star'" size="24"
					:color="isInBookshelf ? '#f59a23' : '#666'" />
				<text>{{ isInBookshelf ? '已加入' : '加入书架' }}</text>

			</view>
			<view class="tool" @click="settingPopup">
				<uv-icon name="setting" size="24" />
				<text>设置</text>
			</view>
			<view class="read-btn" @click="gotostartReadingView">
				<text>立即阅读</text>
			</view>
		</view>
		<!-- 目录弹窗 -->
		<view class="chapter-container">
			<uv-popup zIndex="1" ref="popup" mode="bottom" :mask="false" :safe-area-inset-bottom="false" border-radius="24"
				custom-style="bottom: 120rpx; box-shadow: none;">
				<view class="chapter-popup">
					<view class="popup-header">
						<text>连载中 共1220章</text>
					</view>
					<scroll-view scroll-y class="chapter-scroll">
						<view v-for="(chapter, index) in chapters" :key="index" class="chapter-item">
							<text class="title">{{ chapter.title }}</text>
							<view class="status">
								<text v-if="chapter.free" class="free-tag">免费</text>
								<uv-icon v-else name="lock" size="20" color="#999" />
							</view>
						</view>
					</scroll-view>
				</view>
			</uv-popup>
			<!-- 设置弹窗 -->
			<uv-popup ref="settingPopup" mode="bottom" :mask="false" border-radius="24"
				custom-style="bottom: 120rpx; background: #f8f9fa;">
				<view class="setting-popup">
					<!-- 字号设置 -->
					<view class="setting-item">
						<text>字号</text>
						<view class="font-control">
							<text @click="fontSize--" class="btn">A-</text>
							<text class="value">{{ fontSize }}</text>
							<text @click="fontSize++" class="btn">A+</text>
						</view>
					</view>

					<!-- 背景颜色 -->
					<view class="setting-item">
						<text>背景</text>
						<view class="color-picker">
							<view v-for="color in colors" :class="['color', {active: bgColor === color}]"
								:style="{background: color}" @click="bgColor = color">
							</view>
						</view>
					</view>

					<!-- 翻页模式 -->
					<view class="setting-item">
						<text>翻页</text>
						<view class="page-mode">
							<text v-for="mode in modes" :class="['mode-btn', {active: pageMode === mode}]"
								@click="pageMode = mode">
								{{ mode }}
							</text>
						</view>
					</view>
				</view>
			</uv-popup>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isInBookshelf: false,
				book: {
					cover: '/static/images/bases/baseimg.png',
					title: '斗罗大陆',
					author: '唐家三少',
					detail: '唐门外门弟子唐三，因偷学内门绝学为唐门所不容， 跳崖明志时却',
					category: '玄幻',
					wordCount: 3.8,
					tags: ['重生', '玄幻', '异能'],
					score: 9.0,
					reviewCount: 6.6,
					readingCount: 3.8,
					// 评论样式
					commentCount: 221,
					comments: [{
							avatar: '/static/images/bases/baseimg.png',
							username: '书友_938383737',
							rating: 5,
							content: '很好看的书籍，值得观看',
							time: '2021-09-09 12:09',
							replyCount: 221,
							likeCount: 221
						},
						{
							avatar: '/static/images/bases/baseimg.png',
							username: '书友_938383737',
							rating: 3,
							content: '很好看的书籍，值得观看',
							time: '2021-09-09 12:09',
							replyCount: 221,
							likeCount: 221
						},
						{
							avatar: '/static/images/bases/baseimg.png',
							username: '书友_938383737',
							rating: 2,
							content: '很好看的书籍，值得观看',
							time: '2021-09-09 12:09',
							replyCount: 221,
							likeCount: 221
						}
					]
				},
				chapters: [{

						title: '第一章 斗罗大陆，异界唐三（一）',
						free: true
					},
					{
						title: '第一章 斗罗大陆，异界唐三（二）',
						free: true
					},
					{
						title: '第一章 斗罗大陆，异界唐三（三）',
						free: true
					},
					{
						title: '第一章 斗罗大陆，异界唐三（四）',
						free: true
					},
					{
						title: '第二章 奇茸通天菊',
						free: false
					},
					{
						title: '第三章 星斗大森林',
						free: false
					},
					{
						title: '第一章 斗罗大陆，异界唐三（三）',
						free: true
					},
					{
						title: '第一章 斗罗大陆，异界唐三（四）',
						free: true
					},
					{
						title: '第二章 奇茸通天菊',
						free: false
					},
					{
						title: '第三章 星斗大森林',
						free: false
					}
				],
				// 设置弹窗数据
				fontSize: 21,
				bgColor: '#fff',
				pageMode: '仿真',
				colors: ['#FDF1DC', '#E4F5D8', '#2D2D2D', '#DCE8F5'],
				modes: ['仿真', '上下', '覆盖'],
				//底部操作栏数据
				isOpenList: true,
				isSettingPopup:true
			}
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			gotomorecommentsView() {
				uni.navigateTo({
					url: '/pages/bookcity/morecommentsView'
				})
			},
			gotostartReadingView() {
				uni.navigateTo({
					url: '/pages/bookcity/startReadingView'
				})
			},
			// 目录弹窗打开
			open() {
				if(this.isOpenList){
					this.$refs.popup.open('bottom');
					this.isOpenList = !this.isOpenList
				}else{
					this.$refs.popup.close();
					this.isOpenList = !this.isOpenList
				}
			},
			// 加入书架
			handleBookshelf() {
				this.isInBookshelf = !this.isInBookshelf
				this.$refs.uToast.show({
					message: this.isInBookshelf ? '已加入书架' : '已移除',
					type: this.isInBookshelf ? 'success' : 'warning',
					duration: 1500
				})
			},
			// 设置弹窗方法
			settingPopup() {
				if(this.isSettingPopup){
					this.$refs.settingPopup.open()
					this.isSettingPopup = !this.isSettingPopup
				}else{
					this.$refs.settingPopup.close()
					this.isSettingPopup = !this.isSettingPopup
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		background-color: #f6f8fa;
	}

	/* 头部导航栏样式 */
	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 24rpx 0;
		background: #fff;
		position: relative;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 1000;

		.nav-back {
			width: 88rpx;
			padding-left: 32rpx;
		}

		.header-icon {
			display: flex;
			padding-right: 20rpx;
		}
	}

	/* 书籍主信息 */
	.book-main {
		display: flex;
		padding: 130rpx 32rpx 32rpx 32rpx;
		background-color: #fff;

		.cover {
			width: 200rpx;
			height: 280rpx;
			border-radius: 8rpx;
			margin-right: 32rpx;
		}

		.info {
			flex: 1;

			.title {
				font-size: 40rpx;
				font-weight: bold;
				margin-bottom: 16rpx;
			}

			.author {
				color: #666;
				margin-bottom: 24rpx;
			}

			.meta {
				color: #999;
				font-size: 28rpx;
			}
		}
	}

	/* 标签组 */
	.tag-group {
		display: flex;
		gap: 16rpx;
		padding: 0 32rpx;
		background-color: #fff;

		.tag {
			background-color: #f59a23;
			color: #fff;
			padding: 8rpx 24rpx;
			border-radius: 40rpx;
			font-size: 24rpx;
		}
	}

	/* 评分信息 */
	.rating-info {
		height: 160rpx;
		display: flex;
		justify-content: space-around;
		align-items: center;
		padding: 0 32rpx;
		margin-bottom: 20rpx;
		background-color: #fff;

		.score {
			display: flex;
			flex-direction: column;
			align-items: center;

			.number {
				margin-right: 8rpx;

				text {
					font-size: 48rpx;
				}
			}
		}

		.counts {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-left: 48rpx;
			line-height: 1.6;

			text {
				text {
					font-size: 48rpx;
				}
			}
		}
	}

	/* 书评区块 */
	.reviews {
		padding: 0 32rpx;
		background-color: #fff;

		.header-reviews {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 32rpx;
			padding-top: 20rpx;

			.title {
				font-weight: bold;
				margin-right: 16rpx;
			}

			.count {
				color: #999;
				margin-right: 24rpx;
			}
		}

		.comment-item {
			display: flex;
			padding-bottom: 20rpx;

			.avatar {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				margin-right: 24rpx;
			}

			.content {
				flex: 1;

				.user {
					display: flex;
					align-items: center;
					margin-bottom: 16rpx;
					justify-content: space-between;

					.user-rate {

						.name {
							margin-right: 16rpx;
						}
					}


				}


				.text {
					color: #333;
					line-height: 1.6;
					margin-bottom: 24rpx;
				}

				.footer {
					display: flex;
					justify-content: space-between;
					color: #999;

					.actions {
						display: flex;
						gap: 32rpx;

						.action {
							display: flex;
							align-items: center;
							gap: 8rpx;
						}
					}
				}
			}
		}
	}

	// 查看全部评论
	.view-all {
		display: flex;
		align-items: center;
		justify-content: center;
		background: #fff;
		border-top: 1rpx solid #eee;
		padding-top: 20rpx;
		padding-bottom: 160rpx;

		uv-icon {
			margin-left: 8rpx;
		}
	}

	/* 底部工具栏 */
	.toolbar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background: #fff;
		display: flex;
		align-items: center;
		padding: 16rpx 32rpx;
		box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.05);
		z-index: 999;

		.tool {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			color: #666;
		}

		.read-btn {
			background: #f59a23;
			color: #fff;
			padding: 18rpx 58rpx;
			border-radius: 40rpx;
			margin-left: 32rpx;
		}
	}
	// 目录弹窗
	.chapter-container {
		.chapter-popup {
			height: 60vh;

			.popup-header {
				padding: 32rpx;
				border-bottom: 1rpx solid #eee;
				font-size: 32rpx;
				color: #666;
			}

			.chapter-scroll {
				max-height: calc(60vh - 104rpx);
			}

			.chapter-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 28rpx 32rpx;
				border-bottom: 1rpx solid #eee;

				.title {
					font-size: 28rpx;
					color: #333;
				}

				.free-tag {
					color: #f59a23;
					font-size: 24rpx;
					padding: 4rpx 16rpx;
					background: rgba(245, 154, 35, 0.1);
					border-radius: 24rpx;
				}
			}
		}
	}
	
	// 设置弹窗样式
	.setting-popup {
	  padding: 32rpx;
	  background: #fff;
	
	  .setting-item {
	    margin-bottom: 48rpx;
	    display: flex;
	    justify-content: space-between;
	    align-items: center;
	
	    // 字号控制
	    .font-control {
	      display: flex;
	      align-items: center;
	      .btn {
	        color: #666;
	        padding: 0 20rpx;
	      }
	      .value {
	        font-size: 36rpx;
	        margin: 0 32rpx;
	      }
	    }
	
	    // 颜色选择
	    .color-picker {
	      display: flex;
	      gap: 24rpx;
	      .color {
	        width: 64rpx;
	        height: 64rpx;
	        border-radius: 50%;
	        border: 2rpx solid transparent;
	        &.active {
	          border-color: #f59a23;
	        }
	      }
	    }
	
	    // 翻页模式
	    .page-mode {
	      display: flex;
	      gap: 24rpx;
	      .mode-btn {
	        padding: 12rpx 32rpx;
	        border-radius: 40rpx;
	        background: #eee;
	        &.active {
	          background: #f59a23;
	          color: white;
	        }
	      }
	    }
	  }
	}
</style>